/***
 * 本地存储
 */
;bs.store = (function () {
    var api = {},
        win = window,
        doc = win.document,
        localStorageName = 'localStorage',
        globalStorageName = 'globalStorage',
        storage;
    function __jsonEncode(val){
      if(bs.util.isKeyExists('bs.jsonEncode')){
         return bs.jsonEncode(val);
      }else{
          var _p = bs.using.base + 'data';
          var _m = {
              js:_p + 'bs.json.js'
          }
          bs.using.modules.BsJson = _m;
          bs.using.load('BsJson',function(){
              return bs.jsonEncode(val);
          });
      }
    };
    function __jsonDecode(val) {
        if (bs.util.isKeyExists('bs.jsonDecode')) {
            return bs.jsonDecode(val);
        } else {
            var _p = bs.using.base + 'data';
            var _m = {
                js: _p + 'bs.json.js'
            }
            bs.using.modules.BsJson = _m;
            bs.using.load('BsJson', function () {
                return bs.jsonDecode(val);
            });
        }
    };
    api.set = function (key, value) {
    };
    api.setJson = function (key, value) {
    };
    api.get = function (key) {
    };
    api.getJson = function (key) {
    };
    api.remove = function (key) {
    };
    api.clear = function () {
    };

    if (localStorageName in win && win[localStorageName]) {
        storage = win[localStorageName];
        api.set = function (key, val) {
            storage.setItem(key, val)
        };
        api.setJson = function (key, val) {
            storage.setItem(key, __jsonEncode(val));
        };
        api.get = function (key) {
            return storage.getItem(key)
        };
        api.getJson = function (key) {
            var ret = null;
            var str = storage.getItem(key);
            if (str) ret = $.parseJSON(str);
            return ret;
        };
        api.remove = function (key) {
            storage.removeItem(key)
        };
        api.clear = function () {
            storage.clear()
        };

    } else if (globalStorageName in win && win[globalStorageName]) {
        storage = win[globalStorageName][win.location.hostname];
        api.set = function (key, val) {
            storage[key] = val
        };
        api.setJson = function (key, val) {
            storage[key] = __jsonEncode(val);
        };
        api.get = function (key) {
            return storage[key] && storage[key].value
        };
        api.getJson = function (key) {
            var ret = null;
            var str = storage[key] && storage[key].value;
            if (str) ret = __jsonDecode(str);
            return ret;
        };
        api.remove = function (key) {
            delete storage[key]
        };
        api.clear = function () {
            for (var key in storage) {
                delete storage[key]
            }
        };

    } else if (doc.documentElement.addBehavior) {
        function getStorage() {
            if (storage) {
                return storage
            }
            storage = doc.body.appendChild(doc.createElement('div'));
            storage.style.display = 'none';
            // See http://msdn.microsoft.com/en-us/library/ms531081(v=VS.85).aspx
            // and http://msdn.microsoft.com/en-us/library/ms531424(v=VS.85).aspx
            storage.addBehavior('#default#userData');
            storage.load(localStorageName);
            return storage;
        }

        api.set = function (key, val) {
            var storage = getStorage();
            storage.setAttribute(key, val);
            storage.save(localStorageName);
        };
        api.setJson = function (key, val) {
            var storage = getStorage();
            storage.setAttribute(key, __jsonEncode(val));
            storage.save(localStorageName);
        };
        api.get = function (key) {
            var storage = getStorage();
            return storage.getAttribute(key);
        };
        api.getJson = function (key) {
            var ret = null, storage = getStorage();
            var str = storage.getAttribute(key);
            if (str) ret = $.parseJSON(str);
            return ret;
        };
        api.remove = function (key) {
            var storage = getStorage();
            storage.removeAttribute(key);
            storage.save(localStorageName);
        }
        api.clear = function () {
            var storage = getStorage();
            var attributes = storage.XMLDocument.documentElement.attributes;
            ;
            storage.load(localStorageName);
            for (var i = 0, attr; attr = attributes[i]; i++) {
                storage.removeAttribute(attr.name);
            }
            storage.save(localStorageName);
        }
    }
    return api;
})();